<template>
	<view>
		<!-- 会话名称修改 -->
		<EditSessionName
			ref="editSessionName"
			@confirm="editSessionNameConfirm"
		/>
		<!-- 删除单个会话提示 -->
		<DeleteOneSessionNotice
			v-model="isShowOneSessionNotice"
			@confirm="deleteConverationConfirmFn"
		/>
		<!-- 删除全部会话提示 -->
		<DelectSessionConfirm
			v-model="isShowDeleteAllSessionConfirmNotice"
			@confirm="deleteAllConvertionConfirmFn"
		/>
		<!-- 会话列表 -->
		<benben-popup
			v-model="isShow"
			:mask="true"
			mode='left'
		>
			<!---侧边菜单flex布局开始-->
			<view class="flex flex-direction flex-wrap align-center benben-flex-layout simplifiedPaper_flex_7">
				<view class='flex flex-direction align-stretch simplifiedPaper_fd7_0'>
					<view class='flex align-center simplifiedPaper_fd7_0_c0'>

						<image
							@tap.stop="toSoundModel"
							data-type="navigateTo"
							data-url="/pages/mine/soundModel/soundModel"
							class='simplifiedPaper_fd7_0_c0_c0'
							mode="aspectFit"
							:src='STATIC_URL+"69.png"'
						></image>
						<button
							class='flex flex-wrap align-center justify-center simplifiedPaper_fd7_0_c0_c1'
							style="background-color: transparent;"
							@click.stop="deleteAllConversationsFunc()"
							:loading="clearAllSessionLoading"
						>
							<text class='simplifiedPaper_fd7_0_c0_c1_c0'>删除全部</text>
						</button>
						<!-- <button
							:loading="addSessionLoading"
							class='flex flex-wrap align-center justify-center simplifiedPaper_fd7_0_c0_c2'
							@click.stop="addSession()"
							data-type="navigateTo"
							:data-url="`/pages/mine/dialogue/dialogue`"
						>
							<image
								class='simplifiedPaper_fd7_0_c0_c2_c0'
								mode="aspectFit"
								:src='STATIC_URL+"70.png"'
							></image>
							<text class='simplifiedPaper_fd7_0_c0_c2_c1'>新对话</text>
						</button> -->
					</view>
					<scroll-view
						scroll-y
						style="height: 80vh;"
					>
						<view
							v-for="(item,index) in sessionData"
							:key="index"
							class='flex align-center justify-between simplifiedPaper_fd7_0_c1'
						>
							<view
								class='flex align-center justify-between simplifiedPaper_fd7_0_c1_c0'
								@click="changeCSession(item.aid)"
							>
								<view class='simplifiedPaper_fd7_0_c1_c0_c0'>{{item.title}}</view>
								<image
									class='simplifiedPaper_fd7_0_c1_c0_c1'
									mode="aspectFit"
									:src='STATIC_URL+"71.png"'
									@click.stop="editConversationNameFunc(item)"
								></image>
							</view>
							<!-- <u-loading
								mode="circle"
								v-if="item.delLoading"
							></u-loading> -->
			<!-- 				<image
								v-else
								class='simplifiedPaper_fd7_0_c1_c1'
								mode="aspectFit"
								:src='STATIC_URL+"54.png"'
								@click.stop="deleteConversationFunc(item.aid)"
							></image> -->
						</view>
						<fu-empty v-if="sessionLoading" />
						<fu-empty-ui v-else-if="!sessionData.length" />
					</scroll-view>
					<view
						class='flex flex-wrap align-center simplifiedPaper_fd7_0_c6'
						@click.stop="isShow=false"
					>
						<image
							class='simplifiedPaper_fd7_0_c6_c0'
							mode="aspectFit"
							:src='STATIC_URL+"73.png"'
						></image>
					</view>
				</view>
			</view>
		</benben-popup>

	</view>
</template>
<script>
	/**
	 * @event {Function} changeCSession 切换当前会话
	 * @event {Function} sessionDel 删除会话回调
	 * @event {Function()} notSession 无会话回调
	 */
	import FuEmpty from '@/components/fu-empty/fu-empty.vue'
	import BenbenPopup from '@/components/benben-popup/benben-popup.vue'
	import DelectSessionConfirm from './DelectSessionConfirm.vue'
	import DeleteOneSessionNotice from './DeleteOneSessionNotice.vue'
	import EditSessionName from './EditSessionName.vue'
	export default {
		components: {
			DelectSessionConfirm,
			DeleteOneSessionNotice,
			EditSessionName,
			BenbenPopup,
			FuEmpty,
		},
		props: { value: Boolean, sceneId: String, usedId: String },
		data() {
			return {
				"isShowOneSessionNotice": false,
				"isShowDeleteAllSessionConfirmNotice": false,
				sessionData: [],
				sessionLoading: true,
				addSessionLoading: false,
				delId: "", // 被删除的会话ID
				getSessionTimer: null,
				clearAllSessionLoading: false,
				cSessionId: "",
			}
		},
		computed: {
			isShow: {
				get() { return this.value },
				set(val) { this.$emit('input', val) }
			}
		},
		// created() {
		// 	this.getSessionListFirst();
		// },
		methods: {
			/** 去设置模型 */
			toSoundModel() {
				uni.navigateTo({ url: "/pages/mine/soundModel/soundModel" })
				this.$emit("input", false);
			},
			/** 获取会话列表 */
			getsessionFn() {
				this.sessionLoading = true;
				clearTimeout(this.getSessionTimer);
				this.getSessionTimer = setTimeout(async () => {
					await this.getSessionListApi();

					if (this.sessionData.length)
						this.changeCSession(this.sessionData[0].aid)
					else this.resetSession();
				}, 100);
			},
			/** 首次加载会话列表【用于防止空会话造成的错误】 */
			async getSessionListFirst() {
				this.sessionLoading = true;
				await this.getSessionListApi();
				if (this.sessionData.length)
					this.changeCSession(this.sessionData[0].aid)
				else this.resetSession();
			},
			/** 获取会话列表API */
			async getSessionListApi() {
				try {
					const res = await this.$post('65a74a8a43018', {
						digitlist_id: this.sceneId
					});
					this.sessionData = (res.data || []).map(item => {
						return {
							...item,
							delLoading: false,
						}
					});
				} catch (e) { console.debug(e, 'e') }
				this.sessionLoading = false;
			},
			/** 新增会话 */
			async addSession(tt = "") {
				if (this.addSessionLoading) return;
				this.addSessionLoading = true;
				try {
					await this.$post('6514eb5176d61', { title: tt || '新会话' });
					this.getsessionFn();
					this.$emit("input", false);
				} catch (e) { this.$message.info(e?.msg || '新增会话失败') }
				this.addSessionLoading = false;
			},
			/** 删除全部会话确认 */
			async deleteAllConvertionConfirmFn() {
				if (this.clearAllSessionLoading) return;
				this.clearAllSessionLoading = true;
				try {
					await this.$post('65a74a38df31b', { aid: this.sceneId, type: 2 });
					this.getsessionFn();
					this.sessionData = [];
					this.resetSession();
					this.$emit("input", false)
				} catch (e) { this.$msg(e?.msg || '提交失败', 'e'); }
				this.clearAllSessionLoading = false;
			},
			// 删除会话确认
			async deleteConverationConfirmFn() {
				const index = this.sessionData.findIndex(item => item.aid == this.delId);
				const _info = this.sessionData[index];
				if (_info.delLoading) return;
				try {
					_info.delLoading = true;
					await this.$post('65a74a38df31b', { aid: this.delId, type: 1 });
					this.sessionData.splice(index, 1);

					if (this.delId == this.cSessionId || !this.sessionData.length) {
						if (this.sessionData.length)
							this.changeCSession(this.sessionData[0].aid);
						else this.resetSession();
					}
				} catch (e) { this.$message.info(e?.msg || '删除失败') }
				_info.delLoading = false;
			},
			// 重置会话
			resetSession() {
				this.cSessionId = "";
				this.$emit('notSession');
			},
			// 删除对话
			async deleteConversationFunc(id = '') {
				if (this.clearAllSessionLoading) return this.$message.info('已经删除全部会话，无需单独操作');
				if (this.usedId == id) return this.$msg("当前会话正在进行中，不可删除")
				// this.isShow = false;
				this.isShowOneSessionNotice = true;
				this.delId = id;
			},
			//删除全部对话
			deleteAllConversationsFunc() {
				if (this.clearAllSessionLoading) return;
				if (this.usedId) return this.$msg("有存在进行中的会话不可全部删除")
				this.isShowDeleteAllSessionConfirmNotice = true;
			},
			// 改变当前会话
			changeCSession(id = '') {
				this.cSessionId = id;
				if (id) {
					const info = this.sessionData.find(item => item.aid == id);
					this.$emit('changeCSession', { id, title: info.title });
				}
				this.$emit("input", false);
			},
			//编辑对话名称
			editConversationNameFunc({ aid = '', title = '' }) {
				this.$refs.editSessionName.open(title || '会话名称', aid);
			},
			/** 页面更新当前会话标题 */
			pageEditSessionTitle(title = "") {
				if (!title) return;
				const index = this.sessionData.findIndex(item => item.aid == this.cSessionId)
				this.sessionData[index].title = title;
			},
			/** 更新会话名称 */
			async editSessionNameConfirm({ id = '', title = '' }) {
				const info = this.sessionData.find(item => item.aid == id);
				if (info && title) {
					info.title = title;
					// await this.$post('65a749c392b98', { aid: id, title });
					this.$emit('updateSessionTitlteSuccess', title);
				}
			},
		}
	}
</script>

<style
	lang="scss"
	scoped
>
	@import './../digitalPersonUseMessage.scss';
</style>
